﻿using BusinessEntity;
using BusinessRule;
using learun.util;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Nacos.AspNetCore;
using Newtonsoft.Json.Linq;
using ProjectManApi.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProjectManApi.Controllers
{
    public class FunSetController : MvcControllerBase
    {
        private readonly INacosServerManager _serverManager;
        public FunSetController(INacosServerManager serverManager) : base(serverManager)
        {
            _serverManager = serverManager;
        }

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> SetDicInfoFun(dynamic dicinfojson)
        {
            string strgsid = "";
 
            List<BDicInfoFun> dics = new List<BDicInfoFun>();
            try
            {
                JObject job = (JObject)dicinfojson;


                //获取方法
                strgsid = dicinfojson.gsid;

                if (null != job["dicinfo"])
                    dics = job["dicinfo"].ToObject<BDicInfoFun[]>().ToList();

            }
            catch (Exception ex)
            {
                return Fail("参数转换出错：" + ex.Message);
            }

            try
            {
                if (string.IsNullOrEmpty(strgsid))
                {
                    return Fail("公司id不能为空");
                }

                for (int i = 0; i < dics.Count; i++)
                {
                    dics[i].dwid = strgsid;
                }

                var SelectTable = await FunSetRule.InsertFunSet(dics, strgsid);

                return Success("操作成功");
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }

        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetDicInfoFun(string strgsid)
        {
            try
            {
                if (string.IsNullOrEmpty(strgsid))
                {
                    return Fail("公司id不能为空");
                }

                var SelectTable = await FunSetRule.GetFunSetByGsid(strgsid);

                return Success(SelectTable);
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }
    }
}
